<template>
	<view class="goods-search flex-col">
		<view class="header-wrap">
			<view class="search">
				<u-search v-model="keyword" @focus="showHistory = true" :focus="showHistory" @search="onSearch"
					bg-color="#F4F4F4"></u-search>
			</view>
		</view>
		<view class="content" v-if="showT==1">
			<tabs @change="changeActive" :current="selectIndex">
				<tab :name="item.name" v-for="(item, index) in cateList" :key="index">
					<!-- <cate-list v-if="showCate[index]" :top="174" ref="mescrollItem" :i="index" :index="selectIndex"  :cate="item">
					</cate-list> -->
					<mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
						:up="upOption" :down="{auto: false}" :fixed="false"  v-if="showCate[index]">
						<view class="goods-list">
							<view>
								<!-- <goods-list type="double" :list="goodsList"></goods-list> -->
								<view class="goods-double double flex flex-wrap row-between col-stretch p-l-20 p-r-20">
									<router-link class="m-t-20" v-for="(item, i) in goodsList" :key="i"
										:to="{path: '/bundle_b/pages/note_detail/note_detail', query: {id: item.id}}">
										<view class="item bg-white" :style="{width: width, height: '100%'}">
											<view class="goods-img">
												<u-image :width="width" :height="width" :border-radius="10" :src="item.image"></u-image>
											</view>
											<view class="goods-info">
												<!-- <view class="goods-name line-2" :class="{'store-tag': item.shop_type == 1}">{{item.name}}</view> -->
												<view class="goods-name line-2">{{item.code}}</view>
												<view class="price mt10 row">
													<!-- <price-format :color="colorConfig.primary" class="m-r-10" :first-size="34" :second-size="26"
														:subscript-size="26" :price="item.activity_price || item.min_price"
														:weight="500"></price-format> -->
												<!-- 	<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
														line-through :price="item.market_price || item.price"></price-format> -->
												</view>
											</view>
										</view>
									</router-link>
								</view>
							</view>
						</view>
					</mescroll-uni>
				</tab>
			</tabs>
		</view>
		<view class="cate-two flex" style="height: 100%;" v-if="showT==2">
			<view class="aside bg-white">
				<scroll-view style="height: 100%" scroll-y="true" scroll-with-animation="true" >
					<block v-for="(item, index) in cateList" :key="index">
						<view :class="'one-item sm ' + (index == selectIndex ? 'active' : '')" @click="changeActive(index)">
							<text class="name">{{ item.name||item.title }}</text>
							<view v-if="index == selectIndex" class="active-line bg-primary"></view>
						</view>
					</block>
				</scroll-view>
			</view>
			<view class="main bg-body" style="height: 100%">
				<scroll-view style="height: 100%" scroll-y="true" lower-threshold="100" scroll-with-animation="true" @scrolltolower="getGoodsListFun">
					<view class="main-wrap">
						<view class="goods">
							<view class="goods">
								<view class="goods-list">
									
										<!-- <goods-list type="double" :list="goodsList"></goods-list> -->
										<view class="goods-double double flex flex-wrap row-between col-stretch p-l-20 p-r-20">
											<router-link class="m-t-20" v-for="(item, i) in goodsList" :key="i"
												:to="{path: '/bundle_b/pages/note_detail/note_detail', query: {id: item.id}}">
												<view class="item bg-white" :style="{width: '240rpx', height: '100%'}">
													<view class="goods-img">
														<u-image width="240rpx" height="240rpx" :border-radius="10" :src="item.image"></u-image>
													</view>
													<view class="goods-info">
														<!-- <view class="goods-name line-2" :class="{'store-tag': item.shop_type == 1}">{{item.name}}</view> -->
														<view class="goods-name line-2">{{item.code}}</view>
														<view class="price mt10 row">
															<!-- <price-format :color="colorConfig.primary" class="m-r-10" :first-size="34" :second-size="26"
																:subscript-size="26" :price="item.activity_price || item.min_price"
																:weight="500"></price-format> -->
														<!-- 	<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
																line-through :price="item.market_price || item.price"></price-format> -->
														</view>
													</view>
												</view>
											</router-link>
										</view>
									
								</view>
								<loading-footer :status="status" :slot-empty="true">
									<view slot="empty" class="flex-col col-center" style="padding: 200rpx 0 0">
										<image class="img-null" :src="imgUrl + '/static/images/goods_null.png'"></image>
										<text class="lighter sm">暂无商品</text>
									</view>
								</loading-footer>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsList,
		getSearchpage,
		clearSearch,
		getListByLevelOne,
		getSoureceCatLists,
		getSourceLists,
	} from '@/api/store';
	import {
		mapGetters
	} from 'vuex';
	import {
		loadingType
	} from '@/utils/type';
	import {
		getRect,
		trottle
	} from '@/utils/tools';
	import {
		DISTRIBUTION,
		SID,
		TOKEN
	} from "@/config/cachekey";
	import Cache from '@/utils/cache'

	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";
	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin],
		data() {
			return {
				upOption: {
					auto: false,
					empty: {
						use : true ,
						icon: `${this.$imgUrl}/wechat/static/images/goods_null.png`,
						tip: "暂无商品",
					}
				},
				keyword: '',
				status: loadingType.LOADING,
				page: 1,
				allPages: 1,
				goodsList: [],
				showHistory: false,
				hotList: [],
				historyList: [],
				selectIndex: 0,
				cateList: [],
				id:0,
				width:'347rpx',
				showCate: [],
				showT:2,
				imgUrl:`${this.$imgUrl}/wechat`
			};
		},

		onLoad(options) {
			uni.hideHomeButton()
			this.onSearch = trottle(this.onSearch, 500, this);
			
		},
		
		onShow() {
			/* console.log("cccccccc",!this.isLogin||!Cache.get(TOKEN))
			if (!this.isLogin||!Cache.get(TOKEN)) {
				uni.navigateTo({
					url: '/bundle_b/pages/note_login/note_login'
				});
			} */
			this.getListByLevelOneFun();
			/* if(this.showT==2){
				this.onRefresh()
			} */
			console.log("selectIndex123",this.selectIndex)
			this.$nextTick(function(){
				console.log("cccccccc",!this.isLogin)
				if (!this.isLogin) {
					console.log("ccc",!this.isLogin)
					uni.navigateTo({
						url: '/bundle_b/pages/note_login/note_login'
					});
				}
			})
			uni.hideHomeButton()
			
		},

		watch: {
			// 监听属性
			keyword(value, old) {
				if (!value && !this.id) {
					this.showHistory = true
				}
			},
			showHistory(value) {
				if (value) {
					this.getSearchpageFun();
				}
			},
			cateList: {
				immediate: true,
				handler(val) {
					 this.onRefresh()
					this.showCate = val.map((item, index) => index == this.selectIndex ? true : false)
				}
			},
		},

		computed: {
			...mapGetters(['sysInfo','isLogin','distribution'])
		},
		methods: {
			downCallback() {
				this.onRefresh();
				//this.mescroll.endSuccess();
			},
			upCallback(page) {
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				let {
					goodsList,
					keyword,
					
				} = this;
				const params = {
					page_size: pageSize,
					page_no: pageNum,
					cat_id: this.id||this.cateList[0].id,
					code:keyword,
					sid:Cache.get(SID)||1,
				}
				getSourceLists(params).then(({
					data
				}) => {
					if (page.num == 1) this.goodsList = [];
					let curPageData = data.lists;
					let curPageLen = curPageData.length;
					let hasNext = !!data.more;
					this.goodsList = this.goodsList.concat(curPageData);
					this.mescroll.endSuccess(curPageLen, hasNext);
				}).catch(() => {
					this.mescroll.endErr()
				})
			},
			
			onChange(e) {
				this.keyword = e.value
			},
			
			clearSearchFun() {
				clearSearch().then(res => {
					if (res.code == 1) {
						this.getSearchpageFun();
					}
				});
			},
			
			changeActive(index) {
				this.selectIndex = index
				this.id=this.cateList[index].id
				this.showCate[index] = true
				this.onRefresh()
			},
			getGoodsListFun() {
				let {
					page,
					cateList,
					status,
					selectIndex,
					goodsList,
					keyword,
					allPages
				} = this;
				if(!cateList.length) return
				if (status == loadingType.FINISHED ) return;
				/* if(page==1){
					this.page++;
				} */
				if(page<=allPages){
					this.page++;
				}else{
					return
				}
				getSourceLists({
					page_size: 10,
					page_no: page,
					cat_id: this.id||this.cateList[0].id,
					code:keyword,
					sid:Cache.get(SID)||1,
				}).then(res => {
					if (res.code == 1) {
						let {
							more,
							lists,
						} = res.data;
						this.allPages=Math.ceil(res.data.count/10)
						goodsList.push(...lists);
						if (goodsList.length == 0) {
							this.status = loadingType.EMPTY
							return;
						}
						if (10*page>=res.data.count&&goodsList.length > 0) {
							this.status = loadingType.FINISHED;
						}
					} else {
						this.status = loadingType.ERROR
					}
				});
			},

			getSearchpageFun() {
				getSearchpage().then(res => {
					if (res.code == 1) {
						let {
							history_lists,
							hot_lists
						} = res.data;
						this.hotList = hot_lists
						this.historyList = history_lists
					}
				});
			},

			onClear() {
				if (this.id) {
					this.onRefresh();
				}
			},
			onSearch() {
				this.showHistory = false
				this.$nextTick(() => {
					this.onRefresh()
				})
			},
			onRefresh() {
				this.goodsList = []
				if (this.showT==2) {
					this.status = loadingType.LOADING
					this.page = 1
					this.goodsList = []
					this.getGoodsListFun()
				} else{
					this.mescroll.resetUpScroll(false);
				}
				
			},

			onChangeKeyword(item) {
				this.keyword = item
				this.showHistory = false
				this.onRefresh();
			},
			
			async getListByLevelOneFun() {
				const {
					code,
					data
				} = await getSoureceCatLists();
				if (code == 1) {
					this.cateList = data
					if (this.id) {
						this.currIndex =this.cateList.findIndex((item)=>{
							return this.id==item.id
						})
					}
				}
			},
		}
	};
</script>
<style lang="scss">
	page {
		height: 100%;
		padding: 0;
	}
	.goods-search {
		height: 100%;
		position: relative;
		.header-wrap {
			position: relative;
			z-index: 999;
			.search {
				box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.03);
				position: relative;
				z-index: 1;
			}
		}
		
		.content {
			flex: 1;
			min-height: 0;

			.goods-list {
				overflow: hidden;
				.goods-double {
					.item {
						width: 30rpx;
						border-radius: 10rpx;
						overflow: hidden;
				
						.goods-info {
							padding: 10rpx;
				
							.goods-name {
								width: 100%;
								height: 80rpx;
								line-height: 40rpx;
							}
				
						}
					}
				}
			}
		}
	}
	.cate-two {
		.aside {
			width: 180rpx;
			flex: none;
			height: 100%;
	
			.one-item {
				position: relative;
				text-align: center;
				padding: 26rpx 10rpx;
	
				&.active {
					color: $-color-primary;
					font-size: 26rpx;
					font-weight: bold;
				}
	
				.active-line {
					position: absolute;
					width: 6rpx;
					height: 30rpx;
					left: 4rpx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
	
		.main {
			height: 100%;
			flex: 1;
	
			.main-wrap {
				position: relative;
	
				padding: 20rpx 20rpx 0;
	
				.two-item {
					border-radius: 10rpx;
	
					.title {
						height: 90rpx;
						padding: 0 20rpx;
	
						.line {
							width: 40rpx;
							height: 1px;
							background-color: #BBBBBB;
						}
					}
	
					.three-list {
						align-items: flex-start;
						padding: 0 10rpx;
	
						.three-item {
							width: 33%;
	
							.text {
								text-align: center;
							}
						}
					}
	
				}
			}
			.goods .item {
				border-radius: 14rpx;
			}
		}
	}
</style>
